package com.lingx.model.impl.executor;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.lingx.model.IContext;
import com.lingx.model.IExecutor;
import com.lingx.model.IPerformer;
import com.lingx.service.InterpretService;
import com.lingx.service.LingxService;
import com.lingx.service.ModelService;
import com.lingx.service.QueryService;
import com.lingx.utils.Utils;
import com.lingx.web.api.IApi;
@Component
public class TreeExecutor implements IExecutor,Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7204178817637061177L;
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private QueryService queryService;
	@Resource
	private InterpretService interpretService;
	@Resource
	private ModelService modelService;
	@Resource
	private LingxService lingxService;
	@Override
	public String getId() {
		return "TreeExecutor";
	}

	@Override
	public String getCode() {
		return "TreeExecutor";
	}

	@Override
	public String getName() {
		return "TreeExecutor";
	}

	@Override
	public String getIconCls() {
		return "TreeExecutor";
	}

	@Override
	public Boolean getLeaf() {
		return true;
	}

	@Override
	public String getModelType() {
		return "TreeExecutor";
	}

	@Override
	public void setId(String id) {
		
	}

	@Override
	public void setAuthor(String author) {
		
	}

	@Override
	public void setLastModifyTime(String lastModifyTime) {
		
	}

	@Override
	public Object execute(Map<String, Object> params,IContext context, IPerformer performer) throws Exception {
		Map<String,Object> ret=IApi.getRetMap();
		String sql=this.queryService.getTreeSelectSql(params, context, performer);
		sql=Utils.sqlInjection(sql);
		//System.out.println("=================================TreeExecutor===================================");
		//System.out.println(sql);
		List<Map<String,Object>> list=this.jdbcTemplate.queryForList(sql);
		if(list.size()==0) {
			list=this.jdbcTemplate.queryForList(sql.replace("fid='0' and", ""));
		}
		String textField=this.modelService.getTextField(context.getEntity()).get(0);
		for(Map<String,Object> map:list) {
			map.put("text", map.get(textField));
			if(params.containsKey("lingxSearch")&&"true".equals(params.get("lingxSearch").toString())) {
				map.put("leaf", true);
			}else {
			if (lingxService.queryForInt((String.format("select count(*) from %s where fid='%s' ", context.getEntity().getTableName(),map.get("id")))) > 0) {
				map.put("leaf", false);
			}else{
				map.put("leaf", true);
			}}
			if(map.get("icon_cls")!=null){
				map.put("iconCls", map.get("icon_cls"));
			}
		}
		//System.out.println(JSON.toJSONString(list));
		//rows=this.interpretService.outputFormat(rows, context.getEntity().getFields().getList(), context.getEntity());
		ret.put("data", list);
		//ret.put("total", this.jdbcTemplate.queryForObject(this.queryService.getCountSql(sql), Integer.class));
		return ret;
	}

}
